package org.jkuang.qstar.index.core;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.jkuang.qstar.commons.jni.*;

/**
 * 
 * @author jkuang 2012年11月17日
 *
 */
public class QStarEngine {
	private RIndex jniIndex;
	private QStarIndex index;

	public QStarEngine(boolean ignoreCase, boolean compress, boolean useJavaIndex) {
		if (useJavaIndex) {
			this.index = new QStarIndex(ignoreCase, 1024, 300);
		} else {
			this.jniIndex = new RIndex(compress, ignoreCase);
		}
	}

	public void insert(long key, long sort, List<String> terms, byte[] data) {
		if (this.index != null) {
			this.index.getWriter().insert(key, sort, terms, data);
		} else {
			this.jniIndex.insert(key, sort, terms);
		}
	}

	public void delete(long key) {
		if (this.index != null) {
			this.index.getWriter().delete(key);
		} else {
			this.jniIndex.delete(key);
		}
	}

	public long[] query(String syntax, long _s_sort, long _e_sort, int start, int rows, int count, boolean desc) {
		if (this.index != null) {
			return this.index.getReader().query(syntax, _s_sort, _e_sort, start, rows, count, desc, null,null);
		} else {
			return this.jniIndex.query(syntax, _s_sort, _e_sort, start, rows, count, desc);
		}
	}

	public boolean dump(String fileName) {
		if (this.index != null) {
			return false;
		} else {
			return this.jniIndex.dump(fileName);
		}
	}

	public boolean load(File dir, String fileName) throws IOException {
		if (this.index != null) {
			return this.index.getWriter().loadSnapshot(dir);
		} else {
			return this.jniIndex.load(new File(dir, fileName).getAbsolutePath());
		}
	}
}
